package com.ek.kademlia;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import com.ek.common.Bucket;

public class DefaultBucket implements Bucket {
    private static final long serialVersionUID = 4648114103512937834L;
    private String address;
    private byte[] nodeID;
    private int port;

    public String getAddress() {
        return address;
    }

    public byte[] getNodeID() {
        return nodeID;
    }

    public int getPort() {
        return port;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setNodeID(byte[] nodeID) {
        this.nodeID = nodeID;
    }

    public void setPort(int port) {
        this.port = port;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }
        DefaultBucket rhs = (DefaultBucket) obj;
        return new EqualsBuilder()
                      .append(address, rhs.getAddress())
                      .append(nodeID, rhs.getNodeID())
                      .append(port, rhs.getPort())
                      .isEquals();
    }

    @Override
    public String toString() {
        StringBuffer builder = new StringBuffer(64);
        builder.append("Bucket{\n");
        builder.append("  address:" + address + "\n");
        builder.append("  port:" + port + "\n");
        builder.append("  nodeID:" + new String(nodeID) + "\n");
        builder.append("}\n");
        return builder.toString();
    }
    
    @Override
    public int hashCode() {
        HashCodeBuilder builder = new HashCodeBuilder();
        builder.append(address);
        builder.append(nodeID);
        builder.append(port);
        return builder.toHashCode();
    }

}
